package kr.hkit.androidcomplete01.chaptert08;

import java.util.ArrayList;

import kr.hkit.androidcomplete01.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.StrictMode.VmPolicy;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnLongClickListener;

public class FreeLine extends Activity {
	ArrayList<Vertex> arVertex;
	MyView vm;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		arVertex = new ArrayList<Vertex>();
		vm = new MyView(this);
		vm.setOnLongClickListener(mLongClickListener);
		setContentView(vm);
	}
	
	View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() {
		
		@Override
		public boolean onLongClick(View v) {
			if(vm.paint.getColor() == Color.BLACK){
				vm.paint.setColor(Color.RED);
			} else {
				vm.paint.setColor(Color.GREEN);
			}
			return false;
		}
	};
	
	class MyView extends View {
		Paint paint;
		public MyView(Context context) {
			super(context);
			paint = new Paint(Paint.ANTI_ALIAS_FLAG);
			paint.setColor(Color.BLACK);
			paint.setStrokeWidth(3);
		}
		
		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			canvas.drawColor(Color.LTGRAY);
			
			for(int i = 0; i < arVertex.size(); i++){
				if(arVertex.get(i).Draw){
					canvas.drawLine(arVertex.get(i-1).x, arVertex.get(i-1).y, arVertex.get(i).x, arVertex.get(i).y, paint);
				}
			}
		}

		@Override
		public boolean onTouchEvent(MotionEvent event) {
			super.onTouchEvent(event);
			if(event.getAction() == MotionEvent.ACTION_DOWN){
				arVertex.add(new Vertex(event.getX(), event.getY(), false));
				return true;
			}
			if(event.getAction() == MotionEvent.ACTION_MOVE){
				arVertex.add(new Vertex(event.getX(), event.getY(), true));
				invalidate();
				return true;
			}
			return false;
		}
		
		
		
	}
}
